#include "config.h"

#define DevDS1307	0xD0

/* Converts an uint8 integer to the respective bcd representation
	parameter:
		uint8 x:	The binary representation of the integer
	returns:	The respective bcd representation of the integer (uint8)
*/
uint8 bin2bcd(uint8 x);

/* Converts an uint8 integer in bcd representation to the respective binary representation
	parameter:
		uint8 x:	The bcd representation of the integer
	returns:	The respective binary representation of the integer (uint8)
*/
uint8 bcd2bin(uint8 x);

/* Calculates the time difference in hours between two points of time
	parameter:
		h = hour, d = day, m = month, y = year
		the number behind the single parameter states the point of time (point 1 or point 2)
	returs:	The time difference in hours (int16)
			The difference is positive if point 1 is before point 2
			and negative if point 2 is efore point 1
*/	
int16 getPastHours(uint8 h1, uint8 d1, uint8 m1, uint8 y1,
				   uint8 h2, uint8 d2, uint8 m2, uint8 y2);

/* Sets the time of the real-time clock.
   The seconds are reset to 0.
   To maintain a consistant dosing behaviour, the stored time of the last
   injection is altered by the difference of time between the old and new time.
   	parameter:
		uint8 h: 	hour past since 00:00
		uint8 min:	minutes past since 00:00
*/
void setTime(uint8 h, uint8 min);

/* Sets the date of the real-time clock.
   The day of week field is not altered, becoaus it is not used in this
   application.
   To maintain a consistant dosing behaviour, the stored date of the last
   injection is altered by the difference of time between the old and new date.
   	parameter:
		uint8 y:	The current year
		uint8 m:	The current month
		uint8 d:	The current day of month
*/
void setDate(uint8 d, uint8 m, uint8 y);

/* Initiates the clock. Has to be done at startup to
   ensure that the clock is working:
   The function connects to the real-time clock and reads the
   ch-bit to verify the clock is not disabled.
   If the ch-bit is set, it is set to 0
   to ensure proper behaviour.
*/
void initClock();

/* Reads the date and time registers of the clock and stores the maintaied
   values into the variables given as parameters.
   parameters:
   	uint8* y:	Pointer to the variable where the year will be saved
	uint8* m:	Pointer to the variable where the month will be saved
	uint8* d:	Pointer to the variable where the day of month will be saved
	uint8* h:	Pointer to the variable where the hours will be saved
	uint8* min:	Pointer to the variable where the minutes will be saved
*/
void getTimeAndDate(uint8 *y, uint8 *m, uint8 *d, uint8 *h, uint8 *min);
/* Reads the and time registers of the clock and stores the maintaied
   values into the variables given as parameters.
   parameters:
	uint8* h:	Pointer to the variable where the hours will be saved
	uint8* min:	Pointer to the variable where the minutes will be saved
*/
void getTime(uint8 *h, uint8 *min);

//Array which stores the number of day in the respective month
//(after initialization):
//daysPerMonth[1] returns 31 as the month January has 31 days
//daysPerMonth[0] will return 0
uint8 daysPerMonth[13];
